<template>
  <a-row>
    <a-col flex="200px">
      <div class="title-bar">
        <div class="title">用户中心</div>
      </div>
    </a-col>
    <a-col flex="auto">
      <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="MenuClick" />
    </a-col>
    <a-col flex="250px">
      <div v-if="userStore.loginUser.id">
        <div class="user-info">
          <a-avatar :src="userStore.loginUser.avatar" />
          <div class="user-name">{{ userStore.loginUser.username }}</div>
        </div>
      </div>
      <div v-else class="user-login">
        <a-button type="primary">登录</a-button>
      </div>
    </a-col>
  </a-row>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { CrownOutlined, HomeOutlined } from '@ant-design/icons-vue'
import { type MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'

const router = useRouter()

const userStore = useUserStore()

console.log(userStore.loginUser);


const current = ref<string[]>([])
const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/login/userLogin',
    label: '用户登录',
    title: '用户登录',
  },
  {
    key: '/user/register',
    label: '用户注册',
    title: '用户注册',
  },
  {
    key: '/admin/userManage',
    icon: () => h(CrownOutlined),
    label: '用户管理',
    title: '用户管理',
  },
])

const MenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
router.afterEach((to, from) => {
  current.value = [to.path]
})
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}
.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}
.user-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
</style>
